package com.ficus.road.maintain.ditalTwin;

import com.ficus.road.maintain.core.model.Bbox;
import com.ficus.road.maintain.core.model.Hit;
import com.ficus.road.maintain.core.model.HitAttrs;
import com.ficus.road.maintain.core.model.HitSubject;
import com.ficus.road.maintain.ditalTwin.dao.FrameDao;
import com.ficus.road.maintain.core.model.digitalTwin.Asset;
import com.ficus.road.maintain.core.model.digitalTwin.Frame;
import com.ficus.road.maintain.ditalTwin.service.DigitalRoadService;
import com.ficus.road.maintain.ditalTwin.service.ImportService;
import com.google.common.collect.Lists;
import ficus.car_demo.FrameMeta;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

//@Component
@Slf4j
public class DigitalInit {
    @Autowired
    ImportService importService;
    @Autowired
    FrameDao frameDao;

    @Autowired
    DigitalRoadService digitalRoadService;

    @SneakyThrows
//    @Scheduled(fixedDelay = 1000L)
    public void initByPb() {
        String roadName = "锦绣东路";
        String imagePath = "/home/ficus/Documents/数字孪生/merged_pb_05/";
        File file = new File(imagePath);
        List<File> files = Arrays.stream(file.listFiles()).sorted(Comparator.comparing(File::getName)).collect(Collectors.toList());
        List<FrameMeta.FrameResult> list = Lists.newArrayList();
        int i = 1;
        for (File file1 : files) {
            FrameMeta.FrameResultList frameResultList = FrameMeta.FrameResultList.parseFrom(new FileInputStream(file1));
            FrameMeta.FrameResult frameResult = frameResultList.getResultListList().get(0);
            FrameMeta.FrameResult.Builder builder = frameResult.toBuilder();
            builder.setSequenceId(i++);
            list.add(builder.build());
            if (list.size() == 20){
                importService.importMetaV2(list);
                list.clear();
            }

        }
        System.out.println(1);
    }

    public void mockHit2Frame() {
        List<Frame> all = frameDao.findAll();
        for (Frame frame : all) {
            List<Asset> assets = frame.getAssets();
            if (CollectionUtils.isEmpty(assets)) {
                continue;
            }
            List<Asset> assetsFilter = assets.stream().filter(t -> digitalRoadService.getElementTypes().contains(t.getType())).collect(Collectors.toList());
            frame.setAssets(assetsFilter);

            //mock hit
            Hit hit = new Hit();
            hit.setType("PIC_TRAFFIC_SIGN_DISEASE_RESULT_DISEASE_OCCLUSION");
            hit.setHitTime(frame.getTimestamp());
            hit.setAttrs(HitAttrs.builder()
                    .carId("")
                    .picturePath("http://yitu-aicar.oss-cn-shanghai.aliyuncs.com/disease138/YTKJ20210074/20230325/1679726466278.jpg")
                    .loop(3)
                    .build());
            hit.setSubjects(Lists.newArrayList(HitSubject.builder().bbox(
                            Bbox.builder()
                                    .y(105)
                                    .x(218)
                                    .h(36)
                                    .w(22)
                                    .build())
                    .build()));
            frame.setHits(Lists.newArrayList(hit));
            frameDao.saveOrUpdate(frame);
        }
        System.out.println(2);
    }


}
